﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.Common;
using System.IO;
using BarCode.Module;
using BarCode.AccessDao;
using System.Runtime.InteropServices;
using BarCode.Bean;

namespace BarCode.Forms
{
    public partial class Base_BarCode : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Base_BarCode));


        [DllImport("user32.dll")]
        public static extern bool MessageBeep(uint uType);

        public Base_BarCode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_Load(object sender, EventArgs e)
        {

            this.Text = GlobalVal.FORM_TITLE;
            lblUser.Text = GlobalVal.CurrentUser.User;
            txtBarCode.Focus();


            // 生成备份数据库
            string bakBarcodeMdb = GlobalVal.BK_PATH + "tmpBarcode_" + DateTime.Today.ToString("yyyyMMdd") + ".mdb";
            if (!File.Exists(bakBarcodeMdb))
            {
                if (!Directory.Exists(GlobalVal.BK_PATH))
                {
                    Directory.CreateDirectory(GlobalVal.BK_PATH);
                }
                // 复制文件
                FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "tmpBarcode.mdb", bakBarcodeMdb);
                FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "Empty_barcode.mdb", AppDomain.CurrentDomain.BaseDirectory + "tmpBarcode.mdb");
            }

            // 获取当前用户当天的工作量
            BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
            int workAmount = barcodeMdbDao.getWorkAmount(GlobalVal.CurrentUser.User);
            lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();
            barcodeMdbDao.Release();

            txtBarCode.Text = "";
            txtBarCode.Focus();


        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }




        /// <summary>
        /// 二维码输入后回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)13)
                {
                    string inputBarCode = txtBarCode.Text;


                    // 保存条码信息
                    BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
                    try
                    {
                        // 插入
                        TmpBarCodeInfo barCodeInfo = new TmpBarCodeInfo();
                        barCodeInfo.barcode = inputBarCode;
                        barCodeInfo.optName = GlobalVal.CurrentUser.User;
                        barCodeInfo.optDate = DateTime.Now;
                        barcodeMdbDao.insertBarCode(barCodeInfo);
                        barcodeMdbDao.Release();
                    }
                    catch (Exception ex)
                    {
                        LogUtil.Error("本地数据库处理异常", ex);
                        // 清空输入内容
                        txtBarCode.Text = "";
                        txtBarCode.Focus();

                        return;
                    }


                    // 获取当前用户当天的工作量
                    barcodeMdbDao = new BarCodeMdbDao();
                    int workAmount = barcodeMdbDao.getWorkAmount(GlobalVal.CurrentUser.User);
                    lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();
                    barcodeMdbDao.Release();
                    // 清空输入内容
                    txtBarCode.Text = "";
                    txtBarCode.Focus();
                    lblAmount.ForeColor = Color.Black;
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
            }
        }

        /// <summary>
        /// 输入有误时弹框显示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowInputWarn(string msg, uint beep)
        {
            txtBarCode.Text = "";
            txtBarCode.Focus();
           
            if (beep > 0)
            {
                MessageBeep(beep);
            }
            MessageUtil.Warn(msg);
        }
    }
}
